package com.cq;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExportExcelUtil {
	@SuppressWarnings({ "unused", "deprecation" })
	public static HSSFWorkbook export(List<String> coulumNameList,List<List<Object>> excelList) {  
        try{  
            HSSFWorkbook workbook = new HSSFWorkbook();                     // 创建工作簿对象  
            HSSFSheet sheet = workbook.createSheet();                  // 创建工作表  
            sheet.setDefaultColumnWidth((short) 15);                        //默认宽度
            // 产生表格标题行  
            HSSFRow rowm = sheet.createRow(0);  
            HSSFCell cellTiltle = rowm.createCell(0);  
              
            // 定义所需列数  
            int columnNum = coulumNameList.size();  
            HSSFRow rowRowName = sheet.createRow(0);                // 在索引2的位置创建行(最顶端的行开始的第二行)  
              
            // 将列头设置到sheet的单元格中  
            for(int n=0;n<columnNum;n++){  
                HSSFCell  cellRowName = rowRowName.createCell(n);               //创建列头对应个数的单元格  
                cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING);             //设置列头单元格的数据类型  
                HSSFRichTextString text = new HSSFRichTextString(coulumNameList.get(n));  
                cellRowName.setCellValue(text);                                 //设置列头单元格的值  
            }  
              
            //将查询出的数据设置到sheet对应的单元格中  
            for(int i=0;i<excelList.size();i++){  
                  
                List<Object> obj = excelList.get(i);//遍历每个对象  
                HSSFRow row = sheet.createRow(i+1);//创建所需的行数  
                  
                for(int j=0; j<obj.size(); j++){  
                    HSSFCell  cell = null;   //设置单元格的数据类型  
                    if(j == 0){  
                        cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC);  
                        cell.setCellValue(obj.get(j).toString());  
                    }else{  
                        cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);  
                        if(!"".equals(obj.get(j)) && obj.get(j) != null){  
                            cell.setCellValue(obj.get(j).toString());                       //设置单元格的值  
                            System.out.println(cell.getStringCellValue());
                        }  
                    }  
//                    cell.setCellStyle(style);                                   //设置单元格样式  
                }  
            }  
            //让列宽随着导出的列长自动适应  
            for (int colNum = 0; colNum < columnNum; colNum++) {  
                int columnWidth = sheet.getColumnWidth(colNum) / 256;  
                for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {  
                    HSSFRow currentRow; 
                    System.out.println(sheet.getRow(rowNum));
                    //当前行未被使用过  
                    if (sheet.getRow(rowNum) == null) {  
                        currentRow = sheet.createRow(rowNum);  
                    } else {  
                        currentRow = sheet.getRow(rowNum);  
                    }
                    System.out.println(currentRow.getCell(colNum));
                    if (currentRow.getCell(colNum) != null) {  
                        HSSFCell currentCell = currentRow.getCell(colNum);  
                        if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {  
                            int length = currentCell.getStringCellValue().getBytes().length;  
                            if (columnWidth < length) {  
                                columnWidth = length;  
                            }  
                        }  
                    }  
                }  
                if(colNum == 0){  
                    sheet.setColumnWidth(colNum, (columnWidth-2) * 256);  
                }else{  
                    sheet.setColumnWidth(colNum, (columnWidth+4) * 256);  
                }  
            }  
            return workbook;
        }catch(Exception e){  
            e.printStackTrace(); 
            return null;
        }  
          
    }  

}
